<?php

require_once 'System/Classes/HTML/Form/FormElement.php';
require_once 'System/Classes/HTML/Form/FormNode.php';

class SmartyOutput extends FormElement {
	
	/**
	 * Enter description here...
	 *
	 * @var Smarty
	 */
	private $smarty = null;
	private $variables = array();
	
	public function __construct( $name, $filename, Smarty $smarty, array $variables = array() ) {
		parent::__construct( $name, array( 'value' => $filename ) );
		$this->smarty = $smarty;
		$this->variables = $variables;
	}
	
	public function setVariables( array $variables )
	{
		$this->variables = array_merge( $this->variables, $variables );
	}
	
	public function isValid() {
		return true;
	}
	
	public function getValidator() {
		throw new FormException( 'Can\'t validate non Form compliant element "' . __CLASS__ . '"', 0x800 );
	}
	
	public function __toString() {
		$this->smarty->clear_all_assign();
		$this->smarty->assign( $this->variables );
		$output = '<div id="' . $this->getName() . '" class="smartyoutput ' . $this->getName() . '">' . $this->smarty->fetch( $this->getProperty( 'value' ) ) . '</div>';
		$this->smarty->clear_all_assign();
		return $output;
	}
	
}
